﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NS_DBOP;
using System.Data.SqlClient;

public partial class sysManage_NewsInfo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {
           
            DataSet ds = new DBoperate().ExecuteQuery("select * from NewsType", 0, 0, "table1");
            foreach (DataRow dr in ds.Tables["table1"].Rows)
            {
                this.drpNewType.Items.Add(new ListItem(dr["Type"].ToString(), dr["NTID"].ToString()));
            }
            this.drpNewType.Items.Insert(0, "全部");
            Bind();
        }
        
    }

    public void Bind()
    {
        //string strSql = "select News.Title,NewsType.Type,News.Pubdate from News,NewsType where News.TypeId=NewsType.NTID";
        DataSet ds = new DBoperate().ExecuteQuery("select * from News", 0, 0, "news");
        this.lblCount.Text = ds.Tables[0].Rows.Count.ToString();
        DataView dv = ds.Tables[0].DefaultView;
        PagedDataSource pds = new PagedDataSource();
        AspNetPager1.RecordCount = dv.Count;
        pds.DataSource = dv;
        pds.AllowPaging = true;
        pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
        pds.PageSize = AspNetPager1.PageSize;
        this.GridView1.DataSource = pds;
        this.GridView1.DataBind();
    }

protected void  btnSel_Click(object sender, System.EventArgs e)
{
    
    string title = this.txtTitle.Text.Trim();
    DateTime Fd = Convert.ToDateTime(this.TextBox1.Text);
    DateTime Ed = Convert.ToDateTime(this.TextBox2.Text);
    string strSql = "select News from News where 1=1";
    string type = this.drpNewType.SelectedItem.Value.ToString();
    if (this.drpNewType.SelectedItem.Text=="全部")
    {
        if (Fd.Equals("") && Ed.Equals(""))
        {
            this.RequiredFieldValidator1.Enabled = false;
            this.RequiredFieldValidator2.Enabled = false;
        }
        if (!title.Equals(""))
        {
            strSql += "and itle like '%" + title + "%' and DATEDIFF( dd,Pubdate,'"
                + Fd + "')<=0 and DATEDIFF(dd,Pubdate,'" + Ed + "')>=0";
        }
        else 
        {
            strSql += "and DATEDIFF( dd,Pubdate,'"
                + Fd + "')<=0 and DATEDIFF(dd,Pubdate,'" + Ed + "')>=0";
        }
 
    }
    else
    {
        
            strSql = " and TypeId=" + type;
            if (!title.Equals(""))
            {
                strSql += "and Title like '%" + title + "%' and DATEDIFF( dd,Pubdate,'"
                    + Fd + "')<=0 and DATEDIFF(dd,Pubdate,'" + Ed + "')>=0";
            }
            else
            {
                strSql += "and DATEDIFF( dd,Pubdate,'"
                    + Fd + "')<=0 and DATEDIFF(dd,Pubdate,'" + Ed + "')>=0";
            }
        }

    DataSet ds = new DBoperate().ExecuteQuery(strSql, 0, 0, "tab1");
    this.GridView1.DataSource = ds.Tables[0];
    this.GridView1.DataBind();
    this.lblCount.Text= ds.Tables[0].Rows.Count.ToString();
}
    public string getType(string id)
    {
        string type = new DBoperate().ExecuteScalar("select Type from NewsType where NTID=" + Convert.ToInt32(id)).ToString();
        return type;
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        Bind();
    }
}
